package com.ihighest.app.common.api;

import org.json.JSONArray;
import org.json.JSONObject;

import com.ihighest.app.common.kit.DESKit;
import com.ihighest.app.infox.Const;
import com.lidroid.xutils.http.ResponseInfo;

/**
 * API响应结果
 *
 * @author Vity
 * @date 2014年9月3日
 * @mail vity.1215@gmail.com
 */
public class ApiResult {

    private int status;

    private int code;
    private long time;
    private String msg;
    private String shareId;
    private JSONObject data = null;

    private JSONArray datas = null;

    public ApiResult(ResponseInfo<String> responseInfo) throws Exception {
        super();
        String result = Const.API_ENABLE_ENCRYPTION ? DESKit.decryptDES(responseInfo.result) : responseInfo.result;
        if (result.startsWith("[")) {
            this.datas = new JSONArray(result);
        } else {
            JSONObject json = new JSONObject(result);
            this.status = responseInfo.statusCode;
            if (json.has("code")) {
                this.code = json.getInt("code");
                if (json.has("msg")) {
                    msg = json.getString("msg");
                }
                if (json.has("time")) {
                    this.time = json.getLong("time");
                }
                if (json.has("ShareId")) {
                    this.shareId = json.getString("ShareId");
                }
                if (json.has("data")) {
                    data = json.getJSONObject("data");
                }
                if (json.has("datas")) {
                    datas = json.getJSONArray("datas");
                }

            } else {
                this.code = 100;
                this.data = json;
            }
        }
    }

    /**
     * @return the status
     */
    public int getStatus() {
        return status;
    }

    /**
     * @return the code
     */
    public int getCode() {
        return code;
    }

    /**
     * @return the shareId
     */
    public String getShareId() {
        return shareId;
    }

    /**
     * @return the msg
     */
    public String getMsg() {
        return msg;
    }

    /**
     * @return the msg
     */
    public long getTime() {
        return time;
    }

    /**
     * @return the data
     */
    public JSONObject getData() {
        return data;
    }

    /**
     * @return the datas
     */
    public JSONArray getDatas() {
        return datas;
    }

}
